<script setup>
import { ref, onMounted, reactive } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { findById, save } from "@/api/admin"
import { useAdminStore } from '@/stores/admin'
import { ElMessageBox, ElMessage } from 'element-plus'


const router = useRouter()
const adminStore = useAdminStore()
// 设置请求头的token 和对应的值
const headers = {
  'token': adminStore.getToken
}
// 校验规则
const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 50, message: '长度为3-50', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 15, message: '长度应为6-15个字符', trigger: 'blur' },
  ],
  'employee.employeeId': [
    { required: true, message: '请输入员工ID', trigger: 'blur' },
    { pattern: /^\d+$/, message: '员工ID必须为数字', trigger: 'blur' }
  ]
})

//数据
const ruleForm = reactive({
})

const route = useRoute()
// onMounted钩子函数获取id值
onMounted(() => {
  ruleForm.adminId = route.query.id
  if (ruleForm.adminId) {
    //更新
    findById(ruleForm.adminId).then((res) => {
      ruleForm.username = res.data.username
      ruleForm.password = res.data.password
      ruleForm.employeeId = res.data.employeeId
    })
  }
})

// 获取到el-form元素的原生的dom对象
const ruleFormRef = ref()
const handleSubmitForm = () => {
  // 手动校验每个字段
  if (!ruleForm.username) {
    ElMessage.error('请输入用户名')
    return
  }
  if (!ruleForm.password) {
    ElMessage.error('请输入密码')
    return
  }
  if (!ruleForm.employeeId) {
    ElMessage.error('请输入员工ID')
    return
  }

  // 提交逻辑...
  save(ruleForm).then((res) => {
    ElMessage.success(res.message)
    router.push("/admin/page")
  }).catch(() => {
    ElMessage.error('保存失败')
  })
}

const handleBack = () => {
  router.back()
}
</script>

<template>
  <div id="app">
    <div class="admin-edit-container">
      <div class="admin-header">
        <h1><i class="fas fa-user-shield"></i> 管理员管理系统</h1>
        <p>{{ ruleForm.adminId ? '编辑管理员信息' : '创建新的管理员账户' }}</p>
      </div>
      
      <div class="edit-card">
        <div class="card-header">
          <i class="fas fa-user-cog"></i>
          <h2>{{ ruleForm.adminId ? '编辑管理员' : '新增管理员' }}</h2>
        </div>
        
        <div class="edit-form">
          <div class="form-row">
            <div class="form-item">
              <div class="el-form-item">
                <label class="el-form-item__label">用户名</label>
                <div class="el-form-item__content">
                  <div class="el-input">
                    <span class="el-input__prefix">
                      <i class="fas fa-user"></i>
                    </span>
                    <input 
                      class="el-input__inner"
                      type="text"
                      v-model="ruleForm.username"
                      placeholder="请输入管理员用户名"
                    >
                  </div>
                </div>
              </div>
            </div>
          </div>
          
          <div class="form-row">
            <div class="form-item">
              <div class="el-form-item">
                <label class="el-form-item__label">密码</label>
                <div class="el-form-item__content">
                  <div class="el-input">
                    <span class="el-input__prefix">
                      <i class="fas fa-lock"></i>
                    </span>
                    <input 
                      class="el-input__inner"
                      type="password"
                      v-model="ruleForm.password"
                      placeholder="请输入密码"
                    >
                  </div>
                </div>
              </div>
            </div>
          </div>
          
          <div class="form-row">
            <div class="form-item">
              <div class="el-form-item">
                <label class="el-form-item__label">员工ID</label>
                <div class="el-form-item__content">
                  <div class="el-input">
                    <span class="el-input__prefix">
                      <i class="fas fa-id-card"></i>
                    </span>
                    <input 
                      class="el-input__inner"
                      type="text"
                      v-model="ruleForm.employeeId"
                      placeholder="请输入关联的员工ID"
                    >
                  </div>
                </div>
              </div>
            </div>
          </div>
          
          <div class="form-actions">
            <button class="action-button save" @click="handleSubmitForm">
              <i class="fas fa-save"></i> 保存信息
            </button>
            <button class="action-button back" @click="handleBack">
              <i class="fas fa-arrow-left"></i> 返回
            </button>
          </div>
        </div>
        
        <div class="card-footer">
          管理员管理系统 © 2023 企业名称 | 安全可靠的管理解决方案
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.admin-edit-container {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
}

.admin-header {
  text-align: center;
  margin-bottom: 40px;
  color: rgb(5, 5, 5);
  animation: fadeInDown 0.8s ease;
}

.admin-header h1 {
  font-size: 2.8rem;
  font-weight: 700;
  margin-bottom: 10px;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.admin-header p {
  font-size: 1.2rem;
  opacity: 0.9;
  max-width: 600px;
  margin: 0 auto;
}

.edit-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  backdrop-filter: blur(10px);
  animation: fadeInUp 0.8s ease;
  transform: translateY(0);
  transition: transform 0.3s ease;
}

.edit-card:hover {
  transform: translateY(-5px);
}

.card-header {
  background: linear-gradient(90deg, #409EFF, #64b5ff);
  padding: 25px 30px;
  color: white;
  display: flex;
  align-items: center;
  gap: 15px;
}

.card-header i {
  font-size: 28px;
  background: rgba(0, 0, 0, 0.2);
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-header h2 {
  font-size: 1.8rem;
  font-weight: 600;
}

.edit-form {
  padding: 40px;
}

.form-row {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
}

.form-item {
  flex: 1;
}

.el-form-item {
  margin-bottom: 28px;
}

.el-form-item__label {
  font-weight: 600;
  color: #2c3e50;
  font-size: 16px;
  padding-bottom: 8px;
  display: block;
}

.el-input {
  width: 100%;
}

.el-input__wrapper {
  border-radius: 12px;
  padding: 0 20px;
  height: 50px;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.15);
  transition: all 0.3s ease;
}

.el-input__wrapper:hover {
  box-shadow: 0 2px 12px rgba(64, 158, 255, 0.3);
}

.el-input__prefix {
  left: 12px;
}

.el-input__prefix i {
  color: #409EFF;
  font-size: 18px;
}

.el-input__inner {
  padding-left: 35px;
  font-size: 16px;
  color: #2c3e50;
}

.form-actions {
  display: flex;
  justify-content: center;
  gap: 25px;
  margin-top: 40px;
}

.action-button {
  height: 50px;
  width: 180px;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 1px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.action-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}

.action-button.save {
  background: linear-gradient(90deg, #409EFF, #64b5ff);
  color: white;
  border: none;
}

.action-button.back {
  background: white;
  color: #409EFF;
  border: 2px solid #409EFF;
}

.card-footer {
  text-align: center;
  padding: 20px;
  color: #7f8c8d;
  font-size: 14px;
  border-top: 1px solid #eee;
}

.form-icon {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #409EFF;
  z-index: 10;
  font-size: 18px;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .form-row {
    flex-direction: column;
    gap: 0;
  }
  
  .edit-form {
    padding: 30px 20px;
  }
  
  .card-header {
    padding: 20px;
  }
  
  .form-actions {
    flex-direction: column;
    gap: 15px;
    align-items: center;
  }
  
  .action-button {
    width: 100%;
    max-width: 300px;
  }
  
  .admin-header h1 {
    font-size: 2.2rem;
  }
  
  .admin-header p {
    font-size: 1rem;
  }
}
</style>